<?php include_once './samepart/checkUserLogin.php'; ?>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" type="text/css" href="assets/css/main.css">
<link href="assets/css/bootstrap.min.css" rel="stylesheet">

<script type="text/javascript" src="assets/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<!--    <script type="text/javascript" src="assets/js/asynsign.js"></script>-->
<title>No.2商城</title>

<script type="text/javascript">
    /**
     * 动态修改注销按钮
     **/
    $(function () {//加载用户名 $("#l_login").html() == '登录'
        if ($("#l_login").html() == '登录') {
            $("#l_login").html('<?php echo $_SESSION["username"]  ?>')
            $("#l_login").attr("href", "#");
            $(".showCartHeader").attr("href","cart/cart.php?show_cart_id="+getCartId())
            $("#l_logout").css("display", "block")
        }
    })
    /**
     * getUserId
     */
    function getUserId() {
        var uid = '';
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.User.Login",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'username': '<?php echo $_SESSION["username"] ?>',
                "password": '<?php echo $_SESSION["password"] ?>'
            }),
            dataType: "JSON",
            success: function (result) {
                uid = result['data']['userid'];

                // console.log("uid:"+uid)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return uid;
    }

    /**
     * getCartId
     */
    function getCartId() {
        var carid = ''
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.Cart.getCartInfoByUid",
            type: "GET",
            data: {
                "user_id": getUserId()
            },
            dataType: "JSON",
            success: function (result) {
                carid = result.data.info[0].id;
                // console.log("data:" + username_data)

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return carid;
    }
</script>


<!--header-->
<nav class="navbar navbar-default" role="navigation">
    <button type="button" class="navbar-toggle" data-target="#menu" data-toggle="collapse" aria-expanded="false">
        <span class="sr-only">汉堡按钮</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <div class="navbar-header navbar-left">
        <a href="index.php" class="navbar-brand">No.2商城</a>
    </div>
    <div id="menu" class="collapse navbar-collapse">
        <div class="navbar-left">
            <ul id="goodsType_nav_data" class="nav navbar-nav">
            </ul>
        </div>
        <div class="navbar-right">
            <ul class="nav navbar-nav">
                <li><a id="l_login" href="login/login.php">登录</a></li>
                <li id="l_logout"><a href="index.php?action=logout">注销</a></li>
                <li><a target="_blank" href="login/register.php">注册</a></li>
                <li><a href="order/orderList.php?isOLogin=lg">我的订单</a></li>
                <li><a href="#">消息通知</a></li>
                <li><a class="showCartHeader" href="cart/cart.php?isCLogin=clg">购物车</a></li>
                <li><a id="user_name" href=""></a></li>

            </ul>
        </div>
    </div>
</nav>

<div class="container">
    <form action="" role="form" class="form-horizontal">
        <div class="form-group">
            <div class="col-sm-12 col-xs-12">
                <div class="input-group">
                    <input type="text" name="" id="" class="form-control" placeholder="幸福生活，搜索一下...">
                    <span class="input-group-btn">
                            <input class="btn btn-danger" type="submit" name="" id="" value="开始搜索">
                        </span>
                </div>
            </div>
        </div>
    </form>

    <div class="row">
        <!--轮播图-->
        <div class="col-xs-12 col-sm-12 text-center">
            <div id="myCarousel" class="carousel slide">
                <!-- 轮播（Carousel）指标 -->
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                </ol>
                <!-- 轮播（Carousel）项目 -->
                <div class="carousel-inner">
                    <div class="item active">
                        <img src="assets/image/b1.JPG" alt="First slide">
                    </div>
                    <div class="item">
                        <img src="assets/image/b2.JPG" alt="Second slide">
                    </div>
                    <div class="item">
                        <img src="assets/image/b3.JPG" alt="Third slide">
                    </div>
                </div>
                <!-- 轮播（Carousel）导航 -->
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"> <span
                            class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">上一张</span>
                </a> <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"> <span
                            class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">下一张</span>
                </a>
            </div>
        </div>
    </div>

    <!--热销区商品-->
    <div class="row text-center">
        <div class="fifth col-sm-12">
            <span class="fif_text">热销饮料</span>
        </div>
        <div class="text-center" id="data1">

        </div>
    </div>
    <div class="row text-center">
        <div class="fifth col-sm-12">
            <span class="fif_text">热销饼干</span>
        </div>
        <div class="text-center" id="data2">

        </div>
    </div>
    <div class="row text-center">
        <div class="fifth col-sm-12">
            <span class="fif_text">热销零食</span>
        </div>
        <div class="text-center" id="data3">

        </div>
    </div>

</div>

<?php include_once './samepart/footer.php' ?>

<script type="text/javascript">
    if ($("#l_login").html() == "登录") {
        $("#l_logout").css("display", "none")
    }

    /**
     * 获取商品类型列表
     */
    $(function () {
        $.ajax({
            url: "http://localhost:80/phalapi/public/index.php?s=App.GoodsType.getGoodsTypes",
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                var goodsType_data = result.data.info;
                // console.log("data:" + username_data[0]['name'])
                //<li><a href='goods/goodsList.php'></a></li>

                var s = "";
                for (var i = 0; i < goodsType_data.length; i++) {
                    var goodsType_name = goodsType_data[i]['name'];
                    s += "<li><a href='goods/goodsList.php?actionGoodTypeName=" + goodsType_name +
                        "'> " + goodsType_name +
                        "</a></li>";
                }
                $("#goodsType_nav_data").html(s);
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })

    })

    /**
     * 获取热门商品信息
     */
    $(function () {
        $.ajax({
            url: "http://localhost:80/phalapi/public/index.php?s=App.Goods.getGoodsInfo",
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                var username_data = result.data.info;
                // console.log("data:" + username_data[0]['id'])
                var s = "";
                var pri_url = "./assets/fmwimages/";
                for (var i = 0; i < 4; i++) {
                    var goods_id = username_data[i]['id'];
                    var a = username_data[i]['name'];
                    var b = username_data[i]['picture'];
                    var c = username_data[i]['intro'];
                    var d = username_data[i]['price'];
                    var price_url = pri_url + b
                    // console.log('price_url=' + price_url)
                    s += "<div class='col-xs-6 col-sm-3 '>" +
                        "<a class='siximg img-responsive' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'><img src='" +
                        price_url +
                        "' width='234px' height='234px' class='' />" +
                        "</a> " +
                        "<a class='h4' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'>" + a +
                        "</a>" +
                        "<p class='h6'>" + c + "</p>" +
                        "<p class='pri'>￥" + d + "元</p>" +
                        "</div>";
                }
                $("#data1").html(s);

                var s = "";
                var pri_url = "./assets/fmwimages/";
                for (var i = 4; i < 8; i++) {
                    var goods_id = username_data[i]['id'];
                    var a = username_data[i]['name'];
                    var b = username_data[i]['picture'];
                    var c = username_data[i]['intro'];
                    var d = username_data[i]['price'];
                    var price_url = pri_url + b
                    // console.log('price_url=' + price_url)
                    s += "<div class='col-xs-6 col-sm-3 '>" +
                        "<a class='siximg img-responsive' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'><img src='" +
                        price_url +
                        "' width='234px' height='234px' class='' />" +
                        "</a> " +
                        "<a class='h4' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'>" + a +
                        "</a>" +
                        "<p class='h6'>" + c + "</p>" +
                        "<p class='pri'>￥" + d + "元</p>" +
                        "</div>";
                }
                $("#data2").html(s);

                var s = "";
                var pri_url = "./assets/fmwimages/";
                for (var i = 8; i < 12; i++) {
                    var goods_id = username_data[i]['id'];
                    var a = username_data[i]['name'];
                    var b = username_data[i]['picture'];
                    var c = username_data[i]['intro'];
                    var d = username_data[i]['price'];
                    var price_url = pri_url + b
                    // console.log('price_url=' + price_url)
                    s += "<div class='col-xs-6 col-sm-3 '>" +
                        "<a class='siximg img-responsive' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'><img src='" +
                        price_url +
                        "' width='234px' height='234px' class='' />" +
                        "</a> " +
                        "<a class='h4' href='" +
                        "./goods/goodsDetail.php?actionId=" + goods_id +
                        "'>" + a +
                        "</a>" +
                        "<p class='h6'>" + c + "</p>" +
                        "<p class='pri'>￥" + d + "元</p>" +
                        "</div>";
                }
                $("#data3").html(s);
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })
</script>
